/**
 * @license j2w.socialSubscribeCore.js
 * Adds social subscribe button functionality to the base j2w object.
 *
 * Jobs2Web, INC CONFIDENTIAL
 * ------------------
 * Copyright 2003-2013 Jobs2Web, Inc
 * All Rights Reserved.
 * NOTICE: All information contained herein is, and remain the property of
 * Jobs2web, Inc and its suppliers, if any. The intellectual and
 * technical concepts contained herein are proprietary to Jobs2Web, Inc
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material is
 * strictly forbidden unless prior written permission is obtained from
 * Jobs2Web, Inc.
 *
 * @extends j2w.core.js
 *
 */

var j2w = j2w || {};

j2w.SSB = (function () {
    "use strict";

    // *** PRIVATE ************************************************************

    var _args = {},
        _bHasBeenInitted = false,

        // *** METHODS

        _init = function (args) {
            if (!_bHasBeenInitted) {
                _args = args;

                return (_bHasBeenInitted = true);
            } else {
                return false;
            }
        };

    // *** PUBLIC *************************************************************

    return {
        init : function (args) {
            return _init(args);
        },

        // *** NAMESPACED DEFAULT COLLECTIONS *********************************

        // *** Args - Initialization Arguments ********************************

        Args : {
            get : function (argName) {
                return j2w.Args.get(argName, _args);
            }
        },

        $activeButton : null,
        li : {
            subscribeStarted : false
        },
        presentation : ['drop', 'pop'],
        profileData : []
    };
}());

$(function () {
    "use strict";

    var oSSSelected = j2w.SSB.Args.get('profileData')[0];

    // *** Init Social Networking

    // Prep LinkedIn if it has been loaded.
    if (typeof IN !== 'undefined') {
        IN.Event.on(IN, 'systemReady', ssbLinkedInOnLoad);
    }

    // Facebook init is done in getEndscript.cfm

    // Allow configuration of the social subscribe button presentation methods.
    if (typeof ssbConfiguration === 'object' && ssbConfiguration.presentation) {
        j2w.SSB.presentation = ssbConfiguration.presentation;
    }
});

// *********************************************************************
// ** Social Subscribe Button Generic
// *********************************************************************

    // focusSocialSubscribeInput()
    // Places focus on the SSB e-mail address input.
    function focusSocialSubscribeInput() {
        $('.emailGetter input[type=text]').focus().select();
    }

    // scrollForSSBDropDown()
    // If there are multiple SSBs, and the one that was clicked on is the last one on the page, scroll down to ensure all of it is visible.
    function scrollForSSBDropDown() {
        if ($('ul.btnSocialSubscribe').length > 1 && (j2w.SSB.$activeButton.find('ul.btnSocialSubscribe').data('ssbID') === $('ul.btnSocialSubscribe').last().data('ssbID'))) {
            $('html, body').scrollTop($(document).height());
        }
    }

    // ssbPrepareTo()
    // Positions a social subscribe button based on its presentation method. (pop|drop)
    function ssbPrepareTo(intent) {
        switch (intent) {
            case 'pop':
                j2w.SSB.$activeButton.find('div.socialSubscribe').css({position:'absolute',bottom:j2w.SSB.$activeButton.height() + 'px',left:'',top:''});
                break;

            default:
                j2w.SSB.$activeButton.find('div.socialSubscribe').css({bottom:'',top:'',left:''});
                break;
        }
    }

// *********************************************************************
// ** Facebook-specific Functions
// *********************************************************************

    function socialSubscribeCheckFB() {
        FB.getLoginStatus(function (response) {
            if (response.status === 'connected') {
                socialSubscribeFinishFB();
            } else {
                socialSubscribeLoginFB();
            }
        });
    }

    function socialSubscribeFinishFB() {
        ssbSubscribeHandler({socialSrc : 'fb'});
    }

    function socialSubscribeLoginFB() {
        FB.login(function (response) {
            if (response.status === 'connected') {
                socialSubscribeFinishFB();
            }
        }, {scope: 'email'});
    }

// *********************************************************************
// ** LinkedIn-specific Functions
// *********************************************************************

    function ssbLinkedInOnLoad() {
        // On a LinkedIn auth event, which happens when a user logs in (but also when an already logged-in user visits the page), check to see if the auth came as a result of a user subscribe action or not.
        IN.Event.on(IN,'auth',function () {
            socialSubscribeLoginLI();
        });
    }

    function socialSubscribeCheckLI() {
        // If the user is correctly logged in, finish the subscribe. Otherwise, the user needs to authenticate first.
        if (typeof IN === 'object' && typeof IN.User === 'object' && IN.User.isAuthorized()) {
            socialSubscribeFinishLI();
        } else {
            IN.User.authorize();
        }
    }

    function socialSubscribeFinishLI() {
        ssbSubscribeHandler({socialSrc : 'li'});
    }

    function socialSubscribeLoginLI() {
        // If this auth event was fired because a user just logged in, then check to see if it is OK to finish the subscribe.
        if (j2w.SSB.li.subscribeStarted) {
            socialSubscribeCheckLI();
        }
    }